package com.ctg.itrdc.k8sconsole.control;

import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.imageconsole.common.bean.CommonResult;
import com.ctg.itrdc.imageconsole.common.bean.ReturnCodeDefine;
import com.ctg.itrdc.imageconsole.data.dto.K8sRcDTO;
import com.ctg.itrdc.k8sconsole.service.IDeploymentService;
import com.ctg.itrdc.paspsdk.common.utils.json.JSONUtils;

/**
 * deployment相关接口
 * @Description 
 * @author wupiao
 * @since 2017年3月29日
 */
@Controller
@RequestMapping("/deployment")
public class DeploymentController {

	@Resource
	private IDeploymentService deploymentService;

	private Logger logger = LoggerFactory.getLogger(this.getClass());


	@RequestMapping(value = "/queryDeployments", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryDeployments(@RequestParam("clusterCode") String clusterCode,
			@RequestParam("perpage") String perpage, @RequestParam("page") String page,
			@RequestParam(value = "name", required = false) String name) {

		Map<String, Object> map = null;
		String jsonStr = null;
		for (int i = 0; i < 3; i++) {
			try {
				jsonStr = deploymentService.getDeployments(clusterCode, name, perpage, page);
				map = JSONUtils.jsonToMap(jsonStr);
				break;
			} catch (Exception e) {
				logger.error("queryDeployments出错！", e);
				jsonStr = e.getLocalizedMessage();
			}
		}
		if (map == null || map.size() == 0) {
			return new CommonResult(ReturnCodeDefine.Code.C1101, jsonStr);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, map);
	}
	
	
	@RequestMapping(value = "/queryDeploymentsByNamesp", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryDeploymentsByNamesp(@RequestParam("clusterCode") String clusterCode,
			@RequestParam("perpage") String perpage, @RequestParam("page") String page,
			@RequestParam(value = "name", required = false) String name,
			@RequestParam(value = "namesp", required = false) String namesp) {

		Map<String, Object> map = null;
		String jsonStr = null;
		for (int i = 0; i < 3; i++) {
			try {
				jsonStr = deploymentService.getDeploymentsByNamesp(clusterCode, namesp, name, perpage, page);
				map = JSONUtils.jsonToMap(jsonStr);
				break;
			} catch (Exception e) {
				logger.error("queryDeploymentsByNamesp出错！", e);
				jsonStr = e.getLocalizedMessage();
			}
		}
		if (map == null || map.size() == 0) {
			return new CommonResult(ReturnCodeDefine.Code.C1101, jsonStr);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, map);
	}
	

	@RequestMapping(value = "/queryDeploymentDetail", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult queryDeploymentDetail(@RequestParam("clusterCode") String clusterCode,
			@RequestParam("name") String name, @RequestParam("namesp") String namesp) {

		Map<String, Object> map = null;
		String jsonStr = null;
		for (int i = 0; i < 3; i++) {
			try {
				jsonStr = deploymentService.getDeploymentDetail(clusterCode, namesp, name);
				map = JSONUtils.jsonToMap(jsonStr);
				break;
			} catch (Exception e) {
				logger.error("查询出错！", e);
				jsonStr = e.getLocalizedMessage();
			}
		}
		if (map == null || map.size() == 0) {
			return new CommonResult(ReturnCodeDefine.Code.C1101, jsonStr);
		}
		return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, map);
	}


	@RequestMapping(value = "/updateDpReplicas", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult updateDpReplicas(@RequestBody K8sRcDTO rcDto) {
		boolean isSuccess = false;
		for (int i = 0; i < 3; i++) {
			try {
				deploymentService.updateDpReplicas(rcDto.getClusterCode(), rcDto.getName(), rcDto.getNamesp(),
						rcDto.getReplicas());
				isSuccess = true;
				break;
			} catch (Exception e) {
				logger.error("dp扩缩容时出异常！", e);
				isSuccess = false;
			}
		}
		if (isSuccess) {
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000);
		} else {
			return new CommonResult(ReturnCodeDefine.Code.C1101, "dp扩缩容时出异常！");
		}
	}

}
